package org.dairy.farms.nandana.web.controller;

import org.dairy.farms.model.Lactation;
import org.dairy.farms.nandana.core.NandanaUtils;
import org.dairy.farms.nandana.core.logic.LactationManagementLogic;
import org.dairy.farms.nandana.web.view.LactationView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: GDuggira
 * Date: 22 Jun, 2012
 * Time: 12:17:16 PM
 */
@Controller
public class LactationController {
    @Autowired
    private LactationManagementLogic lactionManagementLogic;

    @Autowired
    private NandanaUtils nandanaUtils;

    @RequestMapping(value = "lactation/lactationDetails.json", method = RequestMethod.GET)
    public void lactationDetails(@RequestParam(value = "id", required = true) Long livestockId, Model model,
                                 @RequestParam(value = "page", required = true) int page,
                                 @RequestParam(value = "start", required = true) int start,
                                 @RequestParam(value = "limit", required = true) int limit) {
        List<Lactation> lactationsList = lactionManagementLogic.getLactationDetails(livestockId, page, start, limit);
        long lacatationsTotalCount = lactionManagementLogic.getLactationDetailsCount(livestockId);
        model.addAttribute("lactationsList", lactationsList);
        model.addAttribute("totalCount", lacatationsTotalCount);
    }

    @RequestMapping(value = "lactation/allLactationDetails.json", method = RequestMethod.GET)
    public void allLactationDetails(@RequestParam(value = "id", required = true) Long livestockId, Model model) {
        List<Lactation> lactationsList = lactionManagementLogic.getAllLactationDetails(livestockId);
        model.addAttribute("lactationsList", lactationsList);
    }


    @RequestMapping(value = "lactation/lactionDetail.json", method = RequestMethod.GET)
    public void lactionDetail(@RequestParam(value = "id", required = true) Long lactationId, Model model) {
        Lactation lactation = lactionManagementLogic.getLactation(lactationId);
        model.addAttribute("lactation", lactation);
    }

    @RequestMapping(value = "lactation/updateLactationDetails.json", method = RequestMethod.POST)
    public void updateLactationDetails(Model model, @RequestParam(value = "dateOfLactation", required = true) String dateOfLactation,
                                       @RequestParam(value = "timeOfLactation", required = true) String timeOfLactation,
                                       @RequestParam(value = "quantity", required = true) int quantity,
                                       @RequestParam(value = "fatPercentage", required = true) int fatPercentage,
                                       @RequestParam(value = "dateOfPayment", required = false) String dateOfPayment,
                                       @RequestParam(value = "ratePerQuantity", required = true) double ratePerQuantity,
                                       @RequestParam(value = "id", required = false) Long lactationId,
                                       @RequestParam(value = "livestockId", required = true) Long livestockId,
                                       @RequestParam(value = "note", required = false) String notes) {
        LactationView lactationView = new LactationView();
        lactationView.setDateOfLactation(nandanaUtils.mergeDateAndTime(dateOfLactation, timeOfLactation));
        lactationView.setDateOfPayment(nandanaUtils.getDate(dateOfPayment));
        lactationView.setFatPercentage(fatPercentage);
        lactationView.setRatePerQuantity(ratePerQuantity);
        lactationView.setId(lactationId);
        lactationView.setQuantity(quantity);
        lactationView.setLivestockId(livestockId);
        lactationView.setNotes(notes);
        Lactation lactation = lactionManagementLogic.updateLactation(lactationView);
        model.addAttribute("lactation", lactation);
    }

}
